﻿$(function () {
    /* Event Handlers for main menu start */
    $("#main-nav .main-nav-link").on('click', function () {
        var $this = $(this);
        var nextSubmenu = $this.next(".main-nav-submenu");

        //apply bacground to clicked link
        $("#main-nav .main-nav-link").not($this).removeClass("active-main-nav-link");
        $this.addClass("active-main-nav-link");

        //set other submenus as invisible
        $("#main-nav .main-nav-submenu").not(nextSubmenu).switchClass("active-submenu", "inactive-submenu", 20);

        //set sub menu of clicked link to visible
        nextSubmenu.switchClass("inactive-submenu", "active-submenu", 20);
        return false;
    });

    $("#main-nav .main-nav-submenu").on('click', function (e) {
        //stop click event from bubbling up to parent elements
        e.stopPropagation();
    });

    $(document).on('click', function (e) {
        //hide active menu when outside of it is clicked
        $("#main-nav .active-submenu").switchClass("active-submenu", "inactive-submenu", 100);

        $("#main-nav .main-nav-link").removeClass("active-main-nav-link");
    });

    /* Event Handlers for main menu end*/

    /* Event Handlers for carousel*/

    var firstCarouselItem = $("#carousel li:first");

    //make first item only visible item
    firstCarouselItem.show();
    $("#carousel li").not(firstCarouselItem).hide();

    //call carousel item switching callback every few seconds
    setInterval(function () {
        var currentVisibleCarouselItem = $("#carousel li:visible"),
            nextCorouselItem = currentVisibleCarouselItem.next("li");

        //if we are at the end of the items list, get to start of list
        if (nextCorouselItem.length == 0) {
            nextCorouselItem = $("#carousel li:first");
        }

        //hide currently visible item and show next item
        nextCorouselItem.zIndex(4);
        currentVisibleCarouselItem.zIndex(3);
        nextCorouselItem.fadeToggle(1000, "swing");
        currentVisibleCarouselItem.fadeToggle(1000, "swing");
    }, 2500);

    /* Event Handlers for carousel end*/
});